package com.xbwang.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

public class SignUtil {

    public static String generatorSign(String body, Map<String, String[]> params, Collection<String> paths
            , String accessKeyId, String accessKeySecret, long timestamp) {
        String allParamsString = getAllParamsString(body, params, paths, accessKeyId, timestamp);
        return generatorSign(allParamsString, accessKeySecret);
    }

    public static String generatorSign(String allParamsString, String accessKeySecret) {
        HMac hMac = new HMac(HmacAlgorithm.HmacSHA256, accessKeySecret.getBytes(StandardCharsets.UTF_8));
        return hMac.digestHex(allParamsString);
    }

    public static String getAllParamsString(String body, Map<String, String[]> params, Collection<String> paths
            , String accessKeyId, long timestamp) {
        StringBuilder sb = new StringBuilder();
        if (StrUtil.isNotBlank(body)) {
            sb.append(body).append('#');
        }

        if (CollectionUtil.isNotEmpty(params)) {
            params.entrySet()
                    .stream()
                    .sorted(Map.Entry.comparingByKey())
                    .forEach(paramEntry -> {
                        String paramValue = Arrays.stream(paramEntry.getValue()).sorted().collect(Collectors.joining(","));
                        sb.append(paramEntry.getKey()).append("=").append(paramValue).append('#');
                    });
        }

        if (CollectionUtil.isNotEmpty(paths)) {
            String pathValues = String.join(",", paths);
            sb.append(pathValues).append('#');
        }

        // 拼接secret和时间戳
        sb.append("accessKeyId=")
                .append(accessKeyId)
                .append("#timestamp=").append(timestamp);
        return sb.toString();
    }

    /**
     * 校验签名
     * @param signature 微信加密签名.
     * @param timestamp 时间戳.
     * @param nonce 随机数.
     * @return
     */
    public static boolean checkSignature(String wxToken,String signature, String timestamp, String nonce) {
        // 对token、timestamp、和nonce按字典排序.
        String[] paramArr = new String[] {wxToken, timestamp, nonce};
        Arrays.sort(paramArr);
        // 将排序后的结果拼接成一个字符串.
        String content  = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);

        String ciphertext = null;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            // 对拼接后的字符串进行sha1加密.
            byte[] digest = md.digest(content.toString().getBytes());
            ciphertext = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // 将sha1加密后的字符串与signature进行对比.
        return ciphertext != null ? ciphertext.equals(signature.toUpperCase()) : false;
    }

    /**
     * 将字节数组转换为十六进制字符串.
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串.
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1' , '2', '3', '4' , '5', '6', '7' , '8', '9', 'A' , 'B', 'C', 'D' , 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }

}
